package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.TSuggestionMapper;
import org.xqyy.eval.dao.TSystemCycleMapper;
import org.xqyy.eval.model.TSuggestion;
import org.xqyy.eval.model.TSystemCycle;
import org.xqyy.eval.model.TUser;
import org.xqyy.eval.service.ITSuggestionService;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TSuggestionService implements ITSuggestionService {

    @Autowired
    private TSuggestionMapper suggestionMapper;
    @Autowired
    private TSystemCycleMapper systemCycleMapper;
    /**
     * 添加合理化意见
     *
     * @param tSuggestion 合理化意见
     * @return
     */
    @Override
    public Boolean addTSuggestion(TSuggestion tSuggestion) {
        suggestionMapper.insert(tSuggestion);
        return true;
    }

    /**
     * 是否允许填写合理化意见
     *
     * @param user 用户
     * @return
     */
    @Override
    public Boolean isAllowSubmitSuggestion(TUser user) {
       List<TSystemCycle> systemCycleList =  systemCycleMapper.selectNowDate();
       if (systemCycleList.size()  == 0){
           return  false;
       }else{
           TSystemCycle systemCycle = systemCycleList.get(0);
           return !isExistTSuggestionByDeptIdAndYearMonth(user.getDeptId(),systemCycle.getCycledate());
       }

    }

    /**
     * 根据科室id和填报周期查找合理化意见
     *
     * @param deptId    科室id
     * @param yearmonth 填报周期
     * @return
     */
    @Override
    public Boolean isExistTSuggestionByDeptIdAndYearMonth(String deptId, String yearmonth) {
        List<TSuggestion> suggestionList = new ArrayList<>();
        Example example = new Example(TSuggestion.class);
        example.createCriteria().andEqualTo("dutyid",deptId)
                .andEqualTo("evalyearmonth",yearmonth);
        suggestionList = suggestionMapper.selectByExample(example);
        if (suggestionList.size()> 0 ){
            return true;
        }else{
            return  false;
        }

    }
}
